import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;


class P540_39643 {

	public static void main(String[] args) {
		
		int counter = 1;
		
		while (true) {
			int teams = Integer.parseInt(readLn());
			if (teams == 0) break;
			
			System.out.println("Scenario #" + counter++);
			
			StringBuilder sb = new StringBuilder();
			
			HashMap<Integer, Integer> e_t = new HashMap<Integer, Integer>();
			
			for (int k = 0; k < teams; k++) {
				
				StringTokenizer st = new StringTokenizer(readLn());
				
				int numberOfElements = Integer.parseInt(st.nextToken());
				
				for (int j = 0; j < numberOfElements; j++) {
					e_t.put(Integer.parseInt(st.nextToken()), k);
				}
			}
			
//			System.out.println(e_t);
			
			String line = readLn();
			
			LinkedList<LinkedList<Integer>> teamQueue = new LinkedList<LinkedList<Integer>>();
			
			while (!"STOP".equals(line)) {
				
				if (line.startsWith("ENQUEUE")) {
					StringTokenizer st = new StringTokenizer(line);
					st.nextToken();
					queue(teamQueue, e_t, Integer.parseInt(st.nextToken()));
				} else if (line.startsWith("DEQUEUE")) {
					sb.append(dequeue(teamQueue) + "\n");
				}
				
				line = readLn();
			}
			
			System.out.println(sb.toString());
			
		}
		

		
	}
	
	private static void queue(LinkedList<LinkedList<Integer>> teamQueue,
			HashMap<Integer, Integer> e_t, int element) {
		
		for (LinkedList<Integer> team : teamQueue) {
			if (e_t.get(team.getFirst()).equals(e_t.get(element))) {
				team.addLast(element);
				return;
			}
		}
		
		LinkedList<Integer> newTeam = new LinkedList<Integer>();
		teamQueue.add(newTeam);
		newTeam.add(element);
	}

	private static Integer dequeue(LinkedList<LinkedList<Integer>> teamQueue) {
		if (!teamQueue.isEmpty()) {
			LinkedList<Integer> team = teamQueue.getFirst();
			Integer result = team.removeFirst();
			if (team.isEmpty()) {
				teamQueue.removeFirst();
			}
			return result;
		}
		
		return null;
	}

	static String readLn() {
        String newLine = System.getProperty("line.separator");
        StringBuffer buffer = new StringBuffer();
        int car = -1;
        try {
            car = System.in.read();
            while ((car > 0) && (car != newLine.charAt(0))) {
                buffer.append((char)car);
                car = System.in.read();
            }
            if (car == newLine.charAt(0))
            System.in.skip(newLine.length() - 1);
        } catch (java.io.IOException e) { return (null);}
        if ((car < 0) && (buffer.length() == 0)) return (null);
        return (buffer.toString()).trim();
    }
}
